package com.platform.destiny.config;

import com.platform.destiny.dto.Result;
import com.platform.destiny.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description
 * @Author kaiyang
 * @Date 2024-01-30 4:34 PM
 */

@Slf4j
@RestControllerAdvice(basePackages = "com.platform.destiny")
public class RestExceptionController {


    @ExceptionHandler(Exception.class)
    public Result<String> handleException(Exception ex) {
        log.error("error ", ex);
        return Result.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
    }


    /**
     * 处理错误信息
     */
    @ExceptionHandler(value = {AppException.class})
    public Result<String> handleException(AppException ex) {
        return Result.failure(ex.getCode(), ex.getMessage());
    }

    /**
     * 无权限访问
     */
    @ExceptionHandler(value = {AccessDeniedException.class})
    @ResponseStatus(HttpStatus.OK)
    public Result<String> handleDeniedException(AccessDeniedException ex) {
        return Result.failure(ex.getMessage());
    }


}
